package com.pactera.madp.cp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.crm.WorkAssetsInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.mapper.crm.WorkAssetsInfoMapper;
import com.pactera.madp.cp.service.WorkAssetsInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class WorkAssetsInfoServiceImpl extends ServiceImpl<WorkAssetsInfoMapper, WorkAssetsInfoEntity> implements WorkAssetsInfoService {

    private final WorkAssetsInfoMapper workAssetsInfoMapper;

    @Override
    public List<WorkAssetsInfoEntity> listAll() {
        List<WorkAssetsInfoEntity> all = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);
        tableNames.forEach(t -> {
            List<WorkAssetsInfoEntity> entityList = workAssetsInfoMapper.list(t);
            if (!entityList.isEmpty()) {
                all.addAll(entityList);
            }
        });
        return all;
    }

    @Override
    public boolean removeAll() {
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);
        tableNames.forEach(t -> workAssetsInfoMapper.removeAll(t));
        return true;
    }

    @Override
    public boolean batchSaveAll(List<WorkAssetsInfoEntity> entityList) {
        Map<String, List<WorkAssetsInfoEntity>> data = specialHandle(entityList);
        if (ObjectUtil.isNull(data)) {
            return false;
        }
        data.forEach((k, v) -> workAssetsInfoMapper.batchSaveOrUpdate(k, v));
        return true;
    }

    @Override
    public List<WorkAssetsInfoEntity> listByCustNo(String custNo) {
        List<WorkAssetsInfoEntity> all = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);
        tableNames.forEach(t -> {
            List<WorkAssetsInfoEntity> entityList = workAssetsInfoMapper.listByCustNo(t,custNo);
            if (!entityList.isEmpty()) {
                all.addAll(entityList);
            }
        });
        return all;
    }

    @Override
    public List<Map<String, Object>> listInCustNos(List<String> custNos) {
        List<Map<String, Object>> allMapList = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);
        tableNames.forEach(t -> {
            List<Map<String, Object>> mapList = workAssetsInfoMapper.listInCustNos(t,custNos);
            //去除空元素
            mapList.removeIf(map->
                    CollectionUtil.isEmpty(map)
            );
            if (!mapList.isEmpty()) {
                allMapList.addAll(mapList);
            }
        });
        return allMapList;
    }

    /**
     * 分表存储
     * @param entityList
     * @return
     */
    private Map<String, List<WorkAssetsInfoEntity>> specialHandle(List<WorkAssetsInfoEntity> entityList) {
        if (CollUtil.isEmpty(entityList)) {
            return null;
        }
        Map<String, List<WorkAssetsInfoEntity>> data = new HashMap<>();
        entityList.forEach(e -> {
            String tableName = SyncUtils.getTableName(e.getCustNo(), Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);
            List<WorkAssetsInfoEntity> list = data.get(tableName);
            if (ObjectUtil.isNull(list)) {
                list = new ArrayList<>();
                list.add(e);
                data.put(tableName, list);
            } else {
                list.add(e);
            }
        });
        return data;
    }
}
